package com.ht.right.dao.impl;

import java.util.List;

import com.ht.right.dao.DeptDao;
import com.ht.right.entity.Dept;

public class DeptDaoImpl extends BaseDaoImpl<Dept> implements DeptDao {
	
	@Override
	public List<Dept> findAllDeptList(){
		String hql="from Dept d order by d.deptNo";
		return super.find(hql);
	}
	@Override
	public List<Dept> findChildDeptList(Dept dept){
		String hql = "from Dept d where d.deptPid='"+dept.getDeptPid()+"' order by d.deptNo ";
		return super.find(hql);
	}

	@Override
	public int updateLaterDeptNo(Dept dept) {
		String hql = "update from Dept d set d.deptNo=(d.deptNo+1) where d.deptPid='"+dept.getDeptPid()+"' and d.deptNo>="+dept.getDeptNo();
		return super.executeHql(hql);
	}

	@Override
	public Object findMaxDeptNo(Dept dept) {
		String hql = "select max(d.deptNo) from Dept d where d.deptPid='"+dept.getDeptPid()+"' ";
		return super.findUnique(hql);
	}
	
	@Override
	public int updatePosition(Dept curDept,Dept laterDept) {
		String hql="";
		if(curDept.getDeptNo()>laterDept.getDeptNo()){
			hql = "update from Dept d set d.deptNo=(d.deptNo+1) where d.deptPid='"+curDept.getDeptPid()+"' and d.deptNo>="+laterDept.getDeptNo()+"and d.deptNo<"+curDept.getDeptNo();
		}else if(curDept.getDeptNo()<laterDept.getDeptNo()){
			hql = "update from Dept d set d.deptNo=(d.deptNo-1) where d.deptPid='"+curDept.getDeptPid()+"' and d.deptNo>"+curDept.getDeptNo()+"and d.deptNo<"+laterDept.getDeptNo();
			
		}
		return super.executeHql(hql);
	}
	
}